/******************************************************************************
COPYRIGHT 2012   : ATECH
Layer            : Signal interaction layer(SIL)
Source File Name : Slave_lin1_sil2.c
Group            : SoftWare Team
Author           : 
Date First Issued: 12/18/2012                                                  
******************************************************************************/
#define Slave_lin1_sil2_c

/* #include */
#include "Slave_lin1_cfg.h"
#include "Slave_lin1_syst.h"
#include "Slave_lin1_dtype.h"
#include "Slave_lin1_htype.h"
#include "Slave_lin1_appl.h"
#include "Slave_lin1_apis.h"

/* arrays */
/* support max 20 ID */
l_u8 LIN1_ID0_Frame[LIN1_ID0_Frame_LENTH];

#if (LIN1_ID_TABLE_SIZE > 1)
l_u8 LIN1_ID1_Frame[LIN1_ID1_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 2)
l_u8 LIN1_ID2_Frame[LIN1_ID2_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 3)
l_u8 LIN1_ID3_Frame[LIN1_ID3_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 4)
l_u8 LIN1_ID4_Frame[LIN1_ID4_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 5)
l_u8 LIN1_ID5_Frame[LIN1_ID5_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 6)
l_u8 LIN1_ID6_Frame[LIN1_ID6_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 7)
l_u8 LIN1_ID7_Frame[LIN1_ID7_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 8)
l_u8 LIN1_ID8_Frame[LIN1_ID8_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 9)
l_u8 LIN1_ID9_Frame[LIN1_ID9_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 10)
l_u8 LIN1_ID10_Frame[LIN1_ID10_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 11)
l_u8 LIN1_ID11_Frame[LIN1_ID11_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 12)
l_u8 LIN1_ID12_Frame[LIN1_ID12_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 13)
l_u8 LIN1_ID13_Frame[LIN1_ID13_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 14)
l_u8 LIN1_ID14_Frame[LIN1_ID14_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 15)
l_u8 LIN1_ID15_Frame[LIN1_ID15_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 16)
l_u8 LIN1_ID16_Frame[LIN1_ID16_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 17)
l_u8 LIN1_ID17_Frame[LIN1_ID17_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 18)
l_u8 LIN1_ID18_Frame[LIN1_ID18_Frame_LENTH];
#endif

#if (LIN1_ID_TABLE_SIZE > 19)
l_u8 LIN1_ID19_Frame[LIN1_ID19_Frame_LENTH];
#endif

l_u8 *LIN1_Frame_Ptr[LIN1_ID_TABLE_SIZE]=
{
    LIN1_ID0_Frame,
    
#if (LIN1_ID_TABLE_SIZE > 1)    
    LIN1_ID1_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 2)
    LIN1_ID2_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 3)
    LIN1_ID3_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 4)
    LIN1_ID4_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 5)
    LIN1_ID5_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 6)
    LIN1_ID6_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 7)
    LIN1_ID7_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 8)
    LIN1_ID8_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 9)
    LIN1_ID9_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 10)
    LIN1_ID10_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 11)
    LIN1_ID11_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 12)
    LIN1_ID12_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 13)
    LIN1_ID13_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 14)
    LIN1_ID14_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 15)
    LIN1_ID15_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 16)
    LIN1_ID16_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 17)
    LIN1_ID17_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 18)
    LIN1_ID18_Frame,
#endif

#if (LIN1_ID_TABLE_SIZE > 19)
    LIN1_ID19_Frame,
#endif
};    
struct lin1_t_id_list LIN1_ID_Table[LIN1_ID_TABLE_SIZE];

/* from the view of Slave to say,It's different from Master */
/* tx buffer and rx buffer */
l_u8 LIN1_Rx_Buffer[1+LIN1_DATASEND_MAX_LEN];/* [?+1]=[DATAmax+CHECKSUM] */
l_u8 LIN1_Tx_Buffer[1+LIN1_DATAREQUEST_MAX_LEN];/* [?+1]=[DATAmax+CHECKSUM] */
/* variables */
l_u8 LIN1_CurrIdIndex = L_ID_INDEX_NOTHING;/* first into lin1_id_receive_message() */


/*****************************************************************************/
/**
 * Function Name: void lin1_api_init(void)
 * Description: LIN api initilzation,include variables,arrays.
 *   
 * Param:   none
 * Return:  none
 * Author:  
 *****************************************************************************/
void lin1_api_init(void)
{
    l_u8 i,j;
    l_u8 len=0;
    struct lin1_t_id_list *ptr2=LIN1_ID_Table;
    
    /* Init ID */
    //---------------------------------------
    len=0;
    for(i=0;i<LIN1_ID_TABLE_SIZE;i++)
    {
        (ptr2+i)->id=LIN1_ROMDatabase[LIN1_ID_START_ADDR+i];
        (ptr2+i)->dir=LIN1_ROMDatabase[LIN1_ID_DIR_START_ADDR+i];
        (ptr2+i)->data_ptr=LIN1_Frame_Ptr[i];
        (ptr2+i)->length=LIN1_ROMDatabase[LIN1_ID_LEN_START_ADDR+i];
        (ptr2+i)->timeout_flag=L_NO;
        (ptr2+i)->timeout_time=LIN1_TIMEOUT_TIME;
        (ptr2+i)->transmitted_flag=L_NO;
        (ptr2+i)->received_flag=L_NO;
        /* set signals default value */
        for(j=0;j<((ptr2+i)->length);j++)
            {*(*(LIN1_Frame_Ptr+i)+j)=LIN1_ROMDatabase[LIN1_INIT_VAL_START_ADDR+len+j];}
        len+=(ptr2+i)->length;    
    }    
}

/*****************************************************************************/
/**
 * Function Name: l_u8 lin1_checksum(l_u8 *data_ptr,l_u8 num,l_u8 protectedId)
 * Description: checksum,for LIN1.3/LIN2.0
 *   
 * Param:   1.data array name 2.data number 3.protectedId
 * Return:  result,one Byte
 * Author:  
 *****************************************************************************/
#pragma MESSAGE DISABLE C5660 
l_u8 lin1_checksum(l_u8 *data_ptr,l_u8 num,l_u8 protectedId)
{
    l_u8 i;
    l_u8 sum;
    l_u8 type;
    
    if((protectedId == DIAG_REQUEST_ID) || (protectedId == DIAG_RESPONSE_ID))
    {
        type = LIN1_DIAGNOSTIC_FRAME_CHECKSUM_TYPE;    
    }
    else
    {
        type = LIN1_UNCONDITIONAL_FRAME_CHECKSUM_TYPE; 
    }
     
    if(type == ENHANCED_CHECKSUM)
    {
        sum=protectedId;  
    }
    else/* CLASSIC_CHECKSUM */
    {
        sum=0; 
    }
  
    for(i=0;i<num;i++)
    {
        sum+=*(data_ptr+i);
        if(sum<*(data_ptr+i)) sum+=1;/* add carry bit */
    }
    return(0xff-sum);
}
#pragma MESSAGE WARNING C5660
/*****************************************************************************/
/**
 * Function Name: void lin1_slave_tick(void)
 * Description:   check rx signal timeout
 *   
 * Param:  none 
 * Return: none 
 * Author:  
 *****************************************************************************/
void lin1_slave_tick(void)
{
    l_u8 i;

	for(i=0; i<LIN1_ID_TABLE_SIZE; i++)
	{
		if(LIN1_ID_Table[i].timeout_time > 0)
		{
			LIN1_ID_Table[i].timeout_time--;
			if(LIN1_ID_Table[i].timeout_time == 0) LIN1_ID_Table[i].timeout_flag = L_YES;
		}
	}
}

